package fi.helsinki.cs.scheduler3000;

import java.util.GregorianCalendar;

/**
 * @author Team TA's
 */


public class Event {

	private String location;
	private String title;
	private GregorianCalendar startTime;
	private GregorianCalendar endTime;
	public static final int[] VALID_START_TIMES = {8, 10, 12, 14, 16, 18};
    public static final int[] VALID_END_TIMES = {10, 12, 14, 16, 18, 20};
    
	private Event(int startTime, int endTime) {
		this.startTime = new GregorianCalendar();
		this.startTime.set(GregorianCalendar.HOUR_OF_DAY ,startTime);
		this.endTime = new GregorianCalendar();
		this.endTime.set(GregorianCalendar.HOUR_OF_DAY, endTime);
	}
	
	public Event(int startTime, int endTime, String title) {
		this(startTime, endTime);
		this.setTitle(title);
	}
	
	public Event(int startTime, int endTime, String title, String location) {
		this(startTime, endTime);
		this.setTitle(title);
		this.setLocation(location);
	}
	
	public String getLocation() {
		return location;
	} 

	public void setLocation(String location) {
		this.location = location;
	}

	public String getStartTime() {
		return ("" + startTime.get(GregorianCalendar.HOUR_OF_DAY));
	}

	public void setStartTime(int startTime) {
		if (checkIfValid(startTime, VALID_START_TIMES)){  
			this.startTime.set(GregorianCalendar.HOUR_OF_DAY, startTime);
		}
		else {
			throw new IllegalArgumentException("Start time must be one of the following: " + getAllValidValues(VALID_START_TIMES));
		}
	}
	
	public String getEndTime() {
		return ("" + endTime.get(GregorianCalendar.HOUR_OF_DAY));
	}

	public void setEndTime(int endTime) {
		if (checkIfValid(endTime, VALID_END_TIMES)){
			this.endTime.set(GregorianCalendar.HOUR_OF_DAY, endTime);
		}
		else {
            throw new IllegalArgumentException("End time must be one of the following: "+ getAllValidValues(VALID_END_TIMES));
		}
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	// PRIVATES
	
	private String getAllValidValues(int[] listOfValids){
        String valids = "";
        for (int v : listOfValids){
                valids += "\""+v+"\", ";
        }
        return valids;
	}
	
	/**
	 * @param value 
	 * @param allValids - Array of ints that contains all valid ints
	 * @return true if value is valid, ie. 
	 * @return false if startTime didn't match a valid start time.
	 */
    private boolean checkIfValid(int value, int[] allValids){
    	for (int valid : allValids){
    		if (value == valid){
    			return true;
    		}
    	}
    	return false;
    }
	
}
